package leetcode;

public class PerfectSquare {

	public static void main(String[] args) {
		PerfectSquare square = new PerfectSquare();
		square.isPerfectSquare(808201);
	}
	public boolean isPerfectSquare(int num) {
		if (num < 0) {
			throw new IllegalArgumentException("the number can't bew negative");
		}
		if (num == 0 || num == 1) {
			return true;
		}
		// binary search
		int left = 0;
		int right = num / 2;
		int middle;
		int temp;
		while (left <= right) {
			middle = left + (right - left) / 2;
			System.out.println("middle: " + middle);
			if ((temp = middle * middle) == num) {
				return true;
			} else if (temp > num) {
				System.out.println("temp more: " + temp);
				right = middle - 1;
			} else {
				System.out.println("temp less: " + temp);
				left = middle + 1;
			}
		}
		return false;
	}
}
